package com.example.blog2023.controller;

import com.example.blog2023.bean.pojo.Blog;
import com.example.blog2023.bean.vo.AdminBlogsVo;
import com.example.blog2023.bean.vo.GroupBlog;
import com.example.blog2023.bean.vo.LabelBlogVo;
import com.example.blog2023.bean.vo.LabelVo;
import com.example.blog2023.service.AdminService;
import com.example.blog2023.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;

/**
 * @author cfy
 * @date 2023/12/14
 **/
@Controller
@RequestMapping("/page")
public class PageController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @GetMapping("/about")
    public ModelAndView getAbout(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/about");
        return modelAndView;
    }
    @GetMapping("/index")
    public ModelAndView getIndex(){
        ArrayList<AdminBlogsVo> allBlogs = adminService.getAllBlogs();
        ArrayList<GroupBlog> groupBlogs = userService.groupTypeList();
        ArrayList<LabelVo> labelVos = userService.groupLabelList();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("allBlogs",allBlogs);
        modelAndView.addObject("labelVos",labelVos);
        modelAndView.addObject("groupBlogs",groupBlogs);
        modelAndView.setViewName("/index");
        return modelAndView;
    }
    @GetMapping("/type/initialize")
    public ModelAndView  typeInitialize(){
        ArrayList<GroupBlog> groupBlogs = userService.groupTypeList();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("groupBlogs",groupBlogs);
        modelAndView.setViewName("/types");
        return modelAndView;
    }
    @GetMapping("/type")
    public ModelAndView getBlogByType(@RequestParam("type") String type){
        ArrayList<GroupBlog> groupBlogs = userService.groupTypeList();
        ArrayList<AdminBlogsVo> blogVos = userService.getBlogByType(type);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("groupBlogs",groupBlogs);
        modelAndView.addObject("blogVos",blogVos);
        modelAndView.setViewName("/types");
        return modelAndView;
    }
//    trgs
    @GetMapping("/label/initialize")
    public ModelAndView labelInitialize(){
        ArrayList<LabelVo> labelVos = userService.groupLabelList();
//        ArrayList<LabelBlogVo> labelByType = userService.getLabelByType(label);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("labelVos",labelVos);
//        modelAndView.addObject("labelByType",labelByType);
        modelAndView.setViewName("/tags");
        return modelAndView;
    }
    @GetMapping("/label")
    public ModelAndView getBlogByLabel(@RequestParam("label") String label){
        ArrayList<LabelVo> labelVos = userService.groupLabelList();
        ArrayList<LabelBlogVo> labelByType = userService.getLabelByType(label);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("labelVos",labelVos);
        modelAndView.addObject("labelByType",labelByType);
        modelAndView.setViewName("/tags");
        return modelAndView;
    }
    @GetMapping("/blog")
    public ModelAndView blogById(@RequestParam("id") Integer id){
        ModelAndView modelAndView = new ModelAndView();
        Blog blog = userService.getBlogById(id);
        modelAndView.addObject("blog",blog);
        modelAndView.setViewName("/blog1");
        return modelAndView;
    }
}
